let getUrlParam = (name,url) => {
  var str = url || window.location.search || window.location.hash,
  result = null;
  if (!name || str === '') {
      return result;
  }
  result = str.match(new RegExp('(^|&|[\?#])' + name + '=([^&]*)(&|$)')); 
  return result === null ? result : decodeURIComponent(result[2]);
}
let u = navigator.userAgent,isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1,isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
let app = {isInAPP:(!!window.myObject) || /youzhijunUserAgent/i.test(u),isInmini: !!getUrlParam('isSP') };//安卓里通过window.myObject判断  ios webview里userAgent加了jingtingUserAgent，在小程序里
const setupWebViewJavascriptBridge = (callback)=>{
  if (window.WebViewJavascriptBridge) { return callback(WebViewJavascriptBridge); }
  if (window.WVJBCallbacks) { return window.WVJBCallbacks.push(callback); }
  window.WVJBCallbacks = [callback];
  var WVJBIframe = document.createElement('iframe');
  WVJBIframe.style.display = 'none';
  WVJBIframe.src = 'https://__bridge_loaded__';
  document.documentElement.appendChild(WVJBIframe);
  setTimeout(function() { document.documentElement.removeChild(WVJBIframe) }, 0)
};
isiOS && /youzhijunUserAgent/i.test(u) && setupWebViewJavascriptBridge(function(bridge) {
  if(bridge){
      app.isInAPP = true;
      app.onShare = (data,fn)=>{//分享自定义链接
        bridge.callHandler('onShare', data,function(response) {
          fn && fn();
        })
      }
      app.scanIdcard = (type)=>{
        return new Promise((resolve,reject)=>{
          bridge.callHandler('scanIdcard', type,function(response) {
            resolve(response);
          })
        })
      }
      app.upload = ()=>{
        return new Promise((resolve,reject)=>{
          bridge.callHandler('selectImg', null,function(response) {
            resolve(response);
          })
        })
      }
      app.getlocate = ()=>{
        return new Promise((resolve,reject)=>{
          bridge.callHandler('getLocate', null,function(response) {
            resolve(response);
          })
        })
      }
      app.setinfo = (data)=>{
        bridge.callHandler('getUserInfo', JSON.stringify(data));
      }
      app.selectcity = ()=>{//切换城市
        return new Promise((resolve,reject)=>{
          bridge.callHandler('selectCityInfo', null,function(response) {
            resolve(response);
          })
        })
      }
      app.wxlogin = ()=>{//微信登录
        return new Promise((resolve,reject)=>{
          bridge.callHandler('wxLogin', null,function(response) {
            resolve(response);
          })
        })
      }
      app.callphone = (phone)=>{//拨打电话
        bridge.callHandler('callSystemPhone', phone);
      }
      app.getUpdateVersion = ()=>{//版本更新
        return new Promise((resolve,reject)=>{
          bridge.callHandler('getUpdateVersion', null,function(response) {
            resolve(response);
          })
        })
      }
      app.getAppVersion = ()=>{
        return new Promise((resolve,reject)=>{
          bridge.callHandler('getAppVersion', null,function(response) {
            resolve(response);
          })
        })
      }
  }
});

if(isAndroid && window.myObject){
  app.isInAPP = true;
  app.scanIdcard = (type)=>{
    return new Promise((resolve,reject)=>{
        window.scanIdcardCallback = function(res){
            resolve(res);
        }
        window.myObject && window.myObject.scanIdcard(type);
    })
  }
  app.upload = ()=>{
    return new Promise((resolve,reject)=>{
        window.selectImgCallback = function(res){
            resolve(res);
        };
        window.myObject && window.myObject.selectImg();
    })
  }
  app.onShare = (data,fn)=>{//分享自定义链接
    window.shareErCodeCallback = function(res){
        fn && fn();
    };
    window.myObject && window.myObject.onShare(JSON.stringify(data));
  }
  app.getlocate = ()=>{
    return new Promise((resolve,reject)=>{
      window.getLocateCallback = function(res){
          resolve(res);
      }
      window.myObject && window.myObject.getLocate();
    })
  }
  app.selectcity = ()=>{//切换城市
    return new Promise((resolve,reject)=>{
      window.selectcityCallback = function(res){
          resolve(res);
      }
      window.myObject && window.myObject.selectcity();
    })
  }
  app.wxlogin = ()=>{//微信登录
    return new Promise((resolve,reject)=>{
      window.wxLoginCallback = function(res){
          resolve(res);
      }
      window.myObject && window.myObject.wxLogin();
    })
  }
  app.setinfo = (data)=>{
    window.myObject && window.myObject.getUserInfo(JSON.stringify(data));
  }
  app.callphone = (phone)=>{//拨打电话
    window.myObject && window.myObject.callphone(phone);
  }
  app.getUpdateVersion = ()=>{//版本更新
    return new Promise((resolve,reject)=>{
      window.getUpdateVersionCallback = function(res){
          resolve(res);
      }
      window.myObject && window.myObject.getUpdateVersion();
    })
  }
  app.getAppVersion = ()=>{
    return new Promise((resolve,reject)=>{
      window.getAppVersionCallback = function(res){
          resolve(res);
      }
      window.myObject && window.myObject.getAppVersion();
    })
  }
}
if(app.isInmini){
  if(localStorage.getItem('notclear')==1){
    localStorage.removeItem('notclear');
  }else{
    localStorage.removeItem("user");
  }
}
export {
  app
};